package com.taijihuabao.modules.service.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.taijihuabao.common.commonutils.BusinessIdUtil;
import com.taijihuabao.modules.entity.*;
import com.taijihuabao.modules.mapper.ModelDataMapper;
import com.taijihuabao.modules.mapper.ModelMapper;
import com.taijihuabao.modules.model.journalmodel.dto.JournalCodeDTO;
import com.taijihuabao.modules.model.journalmodel.dto.ModelDTO;
import com.taijihuabao.modules.model.journalmodel.dto.ModelDataDTO;
import com.taijihuabao.modules.model.journalmodel.vo.ModelDataVO;
import com.taijihuabao.modules.model.journalmodel.vo.ModelVO;
import com.taijihuabao.modules.service.JournalModelRepository;
import com.taijihuabao.modules.service.JournalModelService;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wang、xiao、tu
 */
@Service
public class JournalModelServiceImpl implements JournalModelService {

    @Resource
    ModelMapper modelMapper;
    @Resource
    ModelDataMapper modelDataMapper;
    @Resource
    JournalModelRepository journalModelRepository;
    @Resource
    MongoTemplate mongoTemplate;

    @Override
    public List<ModelVO> getJournalModelList() {
        //声明返回集合
        List<ModelVO> modelList = new ArrayList<>();
        //进行mongo查询
        Query query = new Query();
        query.with(Sort.by(Sort.Order.desc("create_time")));
        List<JournalModelMongoEntity> journalModelMongoList = mongoTemplate.find(query, JournalModelMongoEntity.class);
        if (!CollectionUtils.isEmpty(journalModelMongoList)) {
            for (JournalModelMongoEntity journalMongo : journalModelMongoList) {
                ModelVO modelVO = new ModelVO();
                modelVO.setCode(journalMongo.getCode());
                modelVO.setTitle(journalMongo.getModelTitle());
                modelVO.setCreatorTime(new Date());
                List<ModelDataVO> modelDataListVO = new ArrayList<>();
                if (!CollectionUtils.isEmpty(journalMongo.getFields())) {
                    for (ModelFieldsEntity modelMongo : journalMongo.getFields()) {
                        ModelDataVO modelDataVO = new ModelDataVO();
                        modelDataVO.setCode(modelMongo.getField());
                        modelDataVO.setType(modelMongo.getType());
                        modelDataVO.setText(modelMongo.getTitle());
                        modelDataListVO.add(modelDataVO);
                    }
                    modelVO.setModelDataListVO(modelDataListVO);
                }
                modelList.add(modelVO);
            }
        } else {
            //进行数据库查询，并存入mongo
            List<ModelEntity> journalModelList = modelMapper.getJournalModelList();
            if (!CollectionUtils.isEmpty(journalModelList)) {
                //mongo文档,字段对象
                JournalModelMongoEntity modelMongo = new JournalModelMongoEntity();
                List<ModelFieldsEntity> modelFieldsEntityList = new ArrayList<>();
                for (ModelEntity modelEntity : journalModelList) {
                    //创建modelVo对象，存储模板标题及模板信息集合
                    ModelVO modelVO = new ModelVO();
                    modelVO.setCode(modelEntity.getCode());
                    modelVO.setTitle(modelEntity.getTitle());
                    modelVO.setCreatorTime(new Date());
                    List<ModelDataEntity> modelDataEntityList = modelDataMapper.selectModelDataByModelCode(modelEntity.getCode());
                    List<ModelDataVO> modelDataListVO = new ArrayList<>();
                    for (ModelDataEntity modelDataEntity : modelDataEntityList) {
                        ModelDataVO modelDataVO = new ModelDataVO();
                        modelDataVO.setCode(modelDataEntity.getCode());
                        modelDataVO.setType(modelDataEntity.getType());
                        modelDataVO.setText(modelDataEntity.getText());
                        modelDataListVO.add(modelDataVO);
                        //mongo模板字段
                        ModelFieldsEntity modelFieldsEntity = new ModelFieldsEntity();
                        modelFieldsEntity.setField(modelDataEntity.getCode());
                        modelFieldsEntity.setTitle(modelDataEntity.getText());
                        modelFieldsEntity.setType(modelDataEntity.getType());
                        modelFieldsEntityList.add(modelFieldsEntity);
                    }
                    //赋值返回VO
                    modelVO.setModelDataListVO(modelDataListVO);
                    modelList.add(modelVO);
                    //mongo新增模板
                    modelMongo.setCode(modelEntity.getCode());
                    modelMongo.setModelTitle(modelEntity.getTitle());
                    modelMongo.setFields(modelFieldsEntityList);
                    modelMongo.setCreatorTime(new Date());
                    journalModelRepository.save(modelMongo);
                }
            }
        }
        return modelList;
    }

    @Override
    public JournalModelMongoEntity selectByCode(JournalCodeDTO journalCodeDTO) {
        JournalModelMongoEntity byCode = journalModelRepository.findByCode(journalCodeDTO.getCode());
        return byCode;
    }

    @Override
    public void insertModel(ModelDTO modelDTO) {
//        ModelEntity modelEntity = new ModelEntity();
//        modelEntity.setCode(BusinessIdUtil.createBusinessId("MDL"));
//        modelEntity.setTitle(modelDTO.getTitle());
        //modelMapper.insertModel(modelEntity);
        //mongo 文档对象
        JournalModelMongoEntity modelMongo = new JournalModelMongoEntity();
        //mongo字段集合
        List<ModelFieldsEntity> modelFieldsEntityList = new ArrayList<>();
        //新增模板字段
        for (ModelDataDTO modelDataDTO : modelDTO.getModelDataDTOList()) {
//            ModelDataEntity modelDataEntity = new ModelDataEntity();
//            modelDataEntity.setModelCode(modelEntity.getCode());
//            modelDataEntity.setCode(BusinessIdUtil.createBusinessId("MDT"));
//            modelDataEntity.setType(modelDataDTO.getType());
//            modelDataEntity.setText(modelDataDTO.getText());

//            if ("Radio".equals(modelDataDTO.getType())||"Checkbox".equals(modelDataDTO.getType())) {
//                String optionText = null;
//                for (String option : modelDataDTO.getOptionList()) {
//                    if (optionText == null) {
//                        optionText = option;
//                    } else {
//                        optionText = optionText + "," + option;
//                    }
//                }
//                modelDataEntity.setOptionText(optionText);
//            }
            //modelDataMapper.insertModelData(modelDataEntity);
            //mongo字段
            ModelFieldsEntity modelFieldsEntity = new ModelFieldsEntity();
            modelFieldsEntity.setField(BusinessIdUtil.createBusinessId("MDT"));
            modelFieldsEntity.setTitle(modelDataDTO.getText());
            modelFieldsEntity.setType(modelDataDTO.getType());
            modelFieldsEntity.setOptions(modelDataDTO.getOptions());
            modelFieldsEntityList.add(modelFieldsEntity);
        }
        modelMongo.setCode(BusinessIdUtil.createBusinessId("MDL"));
        modelMongo.setModelTitle(modelDTO.getTitle());
        modelMongo.setFields(modelFieldsEntityList);
        modelMongo.setCreatorTime(new Date());
        //新增mongo文档
        journalModelRepository.save(modelMongo);
    }

    @Override
    public void updateModel(ModelDTO modelDTO) {
        //查询模板
        //ModelEntity model = modelMapper.selectByCode(modelDTO.getCode());
        JournalModelMongoEntity byCode = journalModelRepository.findByCode(modelDTO.getCode());
        //清空模板字段
//        ModelDataEntity modelDataEntity = new ModelDataEntity();
//        modelDataEntity.setModelCode(model.getCode());
//        modelDataMapper.deleteModelDataByModelCode(modelDataEntity);
        journalModelRepository.deleteById(byCode.getId());
        //修改模板
//        model.setTitle(modelDTO.getTitle());
//        modelMapper.updateModel(model);
        byCode.setModelTitle(modelDTO.getTitle());
        byCode.setCode(modelDTO.getCode());
        //mongo字段集合
        List<ModelFieldsEntity> modelFieldsEntityList = new ArrayList<>();
        //新增修改后的字段
        for (ModelDataDTO modelDataDTO : modelDTO.getModelDataDTOList()) {
//            ModelDataEntity modelData = new ModelDataEntity();
//            modelData.setModelCode(model.getCode());
//            modelData.setCode(BusinessIdUtil.createBusinessId("MDT"));
//            modelData.setType(modelDataDTO.getType());
//            modelData.setText(modelDataDTO.getText());
//            if ("Radio".equals(modelDataDTO.getType()) || "Checkbox".equals(modelDataDTO.getType())) {
//                String optionText = null;
//                for (String option : modelDataDTO.getOptionList()) {
//                    if (optionText == null) {
//                        optionText = option;
//                    } else {
//                        optionText = optionText + "," + option;
//                    }
//                }
//                modelData.setOptionText(optionText);
//            }
//                modelDataMapper.insertModelData(modelData);
                //mongo 字段
                ModelFieldsEntity modelFieldsEntity = new ModelFieldsEntity();
                modelFieldsEntity.setField(BusinessIdUtil.createBusinessId("MDT"));
                modelFieldsEntity.setTitle(modelDataDTO.getText());
                modelFieldsEntity.setType(modelDataDTO.getType());
                modelFieldsEntity.setOptions(modelDataDTO.getOptions());
                modelFieldsEntityList.add(modelFieldsEntity);
            }
            //修改mongo文档
//            Query query = Query.query(Criteria.where("code").is(model.getCode()));
//            Update update = new Update();
//            update.set("fields", modelFieldsEntityList);
//            update.set("modelTitle", model.getTitle());
//            mongoTemplate.updateFirst(query, update, JournalModelMongoEntity.class);
        byCode.setFields(modelFieldsEntityList);
        byCode.setCreatorTime(new Date());
        //新增mongo文档
        journalModelRepository.save(byCode);
    }

    @Override
    public void deleteModel(JournalCodeDTO journalCodeDTO) {
//        ModelEntity model = modelMapper.selectByCode(journalCodeDTO.getCode());
//        ModelDataEntity modelDataEntity = new ModelDataEntity();
//        modelDataEntity.setModelCode(model.getCode());
//        modelDataMapper.deleteModelDataByModelCode(modelDataEntity);
//        modelMapper.deleteModel(model);
        //删除mongo文档
        JournalModelMongoEntity byCode = journalModelRepository.findByCode(journalCodeDTO.getCode());
        journalModelRepository.deleteById(byCode.getId());
    }

}
